package com.webshop.DataBase;

import com.webshop.JavaBean.Goods;
import com.webshop.JavaBean.Goods_attr;
import com.webshop.JavaBean.Shopcar;
import com.webshop.Utils.ServletOut;
import com.webshop.service.ShopcarService;
import com.webshop.service.UltimateServie;
import org.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/addShopcar")
public class ShopcarServlet extends javax.servlet.http.HttpServlet {
    ShopcarService addshopcar=new ShopcarService();
    UltimateServie us = new UltimateServie();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method=req.getParameter("method");
        switch (method){
            case "add":
                add(req,resp);
                break;
            case "showshopcar"://展示购物车内容
                show(req,resp);
                break;
            case "delete":
                delete(req,resp);
                break;
        }
    }

    /**
     * 将商品加入购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        Object obj = session.getAttribute("name");
        String name = String.valueOf(obj);
        System.out.println("加购物车");

        String id = request.getParameter("ID");


        int int_id = Integer.parseInt(id);//这里的id本来就是主商品的id
        Goods t = us.backGood(int_id);


        //先返回一个商品的第一个
        Goods_attr temp=  us.onlyGoodsAttr(id);
        temp.setPath(t.getMainPic());//找到path路径我们的目标是显式购物车里的所有图片。

        Shopcar sc = new Shopcar(name, temp.getProductname(), 1, temp.getPath(), temp.getNewPrice(), temp.getID(), temp.getColor());
        addshopcar.insertservice(sc);




    }

    /**
     * 从购物车表里提取发放到我的购物车页面
     * 返回购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("显示");
        HttpSession session=req.getSession();
        Object obj = session.getAttribute("name");
        String name = String.valueOf(obj);
        List<Shopcar> shopcars=new ArrayList<>();
        shopcars= addshopcar.buyfindshop(name);
        PrintWriter writer = resp.getWriter();
       JSONArray jsonArray = new JSONArray();
        jsonArray.put(shopcars);
        writer.write(jsonArray.toString());

    }

    /**
     * 删除购物车物品
     * 需要ID
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
   public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("ID");
        int ID=Integer.parseInt(id);
        try {
            addshopcar.delete(ID);
            ServletOut.request(req,resp,"html/product.jsp");

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("flase");
        }

    }
}
